package cn.iocoder.yudao.module.im.controller.admin.member;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.im.controller.admin.member.vo.*;
import cn.iocoder.yudao.module.im.dal.dataobject.member.MemberMessageDO;
import cn.iocoder.yudao.module.im.service.member.MemberMessageService;

@Tag(name = "管理后台 - IM消息")
@RestController
@RequestMapping("/im/member-message")
@Validated
public class MemberMessageController {

    @Resource
    private MemberMessageService memberMessageService;

    @PostMapping("/create")
    @Operation(summary = "创建IM消息")
    @PreAuthorize("@ss.hasPermission('im:member-message:create')")
    public CommonResult<Long> createMemberMessage(@Valid @RequestBody MemberMessageSaveReqVO createReqVO) {
        return success(memberMessageService.createMemberMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新IM消息")
    @PreAuthorize("@ss.hasPermission('im:member-message:update')")
    public CommonResult<Boolean> updateMemberMessage(@Valid @RequestBody MemberMessageSaveReqVO updateReqVO) {
        memberMessageService.updateMemberMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除IM消息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('im:member-message:delete')")
    public CommonResult<Boolean> deleteMemberMessage(@RequestParam("id") Long id) {
        memberMessageService.deleteMemberMessage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得IM消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('im:member-message:query')")
    public CommonResult<MemberMessageRespVO> getMemberMessage(@RequestParam("id") Long id) {
        MemberMessageDO memberMessage = memberMessageService.getMemberMessage(id);
        return success(BeanUtils.toBean(memberMessage, MemberMessageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得IM消息分页")
    @PreAuthorize("@ss.hasPermission('im:member-message:query')")
    public CommonResult<PageResult<MemberMessageRespVO>> getMemberMessagePage(@Valid MemberMessagePageReqVO pageReqVO) {
        PageResult<MemberMessageDO> pageResult = memberMessageService.getMemberMessagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberMessageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出IM消息 Excel")
    @PreAuthorize("@ss.hasPermission('im:member-message:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMemberMessageExcel(@Valid MemberMessagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberMessageDO> list = memberMessageService.getMemberMessagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "IM消息.xls", "数据", MemberMessageRespVO.class,
                        BeanUtils.toBean(list, MemberMessageRespVO.class));
    }

}